<template>
    <div class="login-container">
        <div class="login-left">
            <img src="../assets/imgs/login_img.png">
        </div>
        <div class="login-box">
            <!-- 头像 -->
            <div class="avatar-box">
                <img src="../assets/imgs/login_logo.png">
            </div>
            <!-- 标题 -->
            <div class="title">
                电商后台管理系统
            </div>
            <!-- 登陆表单 -->
            <el-form ref="loginFormRef" class="login-form" :model="loginForm" :rules="rules">
                <el-form-item prop="username">
                    <el-input placeholder="请输入用户名" prefix-icon="Avatar" v-model="loginForm.username">
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input placeholder="请输入密码" show-password prefix-icon="Lock" v-model="loginForm.password">
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" class="login-btn" @click="loginHandle($refs.loginFormRef)">登陆</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>


</template>

<script  setup >
import { ref, reactive } from 'vue';
import { login } from '@/apis/user'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/user'
const loginForm = reactive({
    username: 'admin',
    password: '123456'
})
// 表单的校验规则
const rules = {
    username: [
        { required: true, message: '请输入登陆账户', trigger: 'blur' },
        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入登陆密码', trigger: 'blur' },
        { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
    ]
}
const loginFormRef = ref(null)
const router = useRouter()
const user = useUserStore()
const loginHandle = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid) => {
        if (valid) {
            login(loginForm).then(res => {
                console.log(res);
                if (res.meta.status == '200') {
                    user.setUserName(res.data.username)
                    router.push('/welcome')
                }
            })
        }
    })
}
</script>

<style scoped lang="scss">
.login-container {
    background: #93defe;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    .login-left {
        margin-right: 100px;
    }

    .login-box {
        width: 400px;
        height: 350px;
        background: #fff;
        padding: 50px;
        border-radius: 6px;
        box-sizing: border-box;
        position: relative;

        .avatar-box {
            width: 120px;
            height: 120px;
            border: 5px solid #93defe;
            border-radius: 100px;
            text-align: center;
            line-height: 110px;
            position: absolute;
            top: -60px;
            right: 140px;
            background: #fff;
        }

        .title {
            position: absolute;
            top: 100px;
            left: 50%;
            transform: translate(-50%);
            font-size: 18px;
            color: #444;
        }

        .login-form {
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translate(-50%);
            padding: 0 20px;
            width: 80%;
            box-sizing: border-box;

            .login-btn {
                width: 100%;
            }
        }
    }
}
</style>
